package com.hotel.front;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.Util;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.gson.Gson;
import com.hotel.BL.ReservationLogic;
import com.hotel.BL.TravelLogic;

import data.Group;
import data.Reservation;
import data.travel;
import data.passingData.ResultData;

@SuppressWarnings("serial")
public class TravelService extends HttpServlet {
	
	SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm",Locale.ENGLISH);
	//http://localhost:8080/modearrivalService?func=addModeArrival&title=1&description=aa
	//http://localhost:8080/modearrivalService?func=editModeArrival&modearrivalId=874&title=12345&description=aabbb
	//http://localhost:8080/modearrivalService?func=delModeArrival&modearrivalId=874
	//http://localhost:8080/modearrivalService?func=changeStatus&modearrivalId=874
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		try{
			String doClass = req.getParameter("func");
			if(doClass.equals("addtravel")){
				Addtravel(req,resp);
			}else if(doClass.equals("edittravel")){
				Edittravel(req,resp);
			}else if(doClass.equals("deltravel")){
				Deletetravel(req,resp);
			}else if(doClass.equals("changeStatus")){
				ChangeStatus(req,resp);
			}else if(doClass.equals("loadtravelG")){
				Loadtravel(req,resp);
			}else if(doClass.equals("loadtravelN")){
				LoadtravelNoneGroup(req,resp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void Addtravel(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException, ParseException
	{	
		Gson gson = new Gson();
		HttpSession session=req.getSession(true);
		long hotelId = (Long) session.getAttribute("hotelId");
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long groupId = Long.parseLong(req.getParameter("groupId"));
		String resId = req.getParameter("resId");
		String tarvelby = req.getParameter("tarvelby");
		String arrive = req.getParameter("arrive");
		boolean showTime = Boolean.parseBoolean(req.getParameter("showTime"));
		boolean showTimeBack = Boolean.parseBoolean(req.getParameter("showTimeBack"));
		
		Date time = formatter.parse(req.getParameter("time"));
		boolean addTask =Boolean.parseBoolean(req.getParameter("addTask"));
		String tarvelbyBack = req.getParameter("tarvelbyBack");
		String arriveBack = req.getParameter("arriveBack");
		
		Date timeBack = formatter.parse(req.getParameter("timeBack"));
		boolean addTaskBack =Boolean.parseBoolean(req.getParameter("addTaskBack"));
		travel obj = new TravelLogic().Addtravel(hotelId, tarvelby, arrive, time, addTask, tarvelbyBack, arriveBack, timeBack, addTaskBack,showTime,showTimeBack);
		String travelId = String.valueOf(obj.getId());
		ReservationLogic resLogic = new ReservationLogic(req,resp);
		resLogic.EditTravelGroup(hotelId, groupId, travelId,resId);
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}

	private void Edittravel(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException, ParseException
	{	
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long id = Long.parseLong(req.getParameter("id"));
		String tarvelby = req.getParameter("tarvelby");
		String arrive = req.getParameter("arrive");
		Date time = formatter.parse(req.getParameter("time"));
		boolean showTime = Boolean.parseBoolean(req.getParameter("showTime"));
		boolean showTimeBack = Boolean.parseBoolean(req.getParameter("showTimeBack"));
		boolean addTask =Boolean.parseBoolean(req.getParameter("addTask"));
		String tarvelbyBack = req.getParameter("tarvelbyBack");
		String arriveBack = req.getParameter("arriveBack");
	
		Date timeBack = formatter.parse(req.getParameter("timeBack"));
		boolean addTaskBack =Boolean.parseBoolean(req.getParameter("addTaskBack"));
		travel obj = new TravelLogic().Edittravel(id, tarvelby, arrive, time, addTask, tarvelbyBack, arriveBack, timeBack, addTaskBack,showTime,showTimeBack);
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}

	private void Deletetravel(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		long id = Long.parseLong(req.getParameter("id"));
		
		Object obj = new TravelLogic().Deletetravel(id);
		Gson gson = new Gson();

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	
	private void ChangeStatus(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		long id = Long.parseLong(req.getParameter("id"));

		Object obj = new TravelLogic().ChangeStatus(id);
		Gson gson = new Gson();

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	private void Loadtravel(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		long groupId = Long.parseLong(req.getParameter("groupId"));
		travel tl = new travel();
		Group g = new ReservationLogic(req,resp).GetGroup(groupId);
	
		
		
		if (g.getTravelString().equals("0") || g.getTravelString() == null) {
			tl = null;
		} else {
			tl = new TravelLogic().Gettravel(Long.parseLong(g
					.getTravelString()));
		}
		
		Gson gson = new Gson();

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(tl));
	}
	
	
	private void LoadtravelNoneGroup(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		long resId =  Long.parseLong(req.getParameter("resId"));
		travel tl = new travel();
		Reservation res = new  ReservationLogic(req,resp).GetReservation(resId);
		
		
			if (res.getTravelString()=="0" || res.getTravelString()== null) {
				tl = null;
			} else {
				tl = new TravelLogic().Gettravel(Long.parseLong(res
						.getTravelString()));
			}
			
		Gson gson = new Gson();

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(tl));
	}
}
